<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** Posts models **/
class Posts extends CI_Model {
	private $posts   = 'posts';
	private $users   = 'employee';
	private $comment = 'post_comment';
	private $images  = 'images';
	
	function getAllRecords($category='',$limit=10,$week=''){
		$this->db->select("*,u.EmployeeNickName as user_name,DAY(post_date) as post_day");
		$this->db->select("MONTH(post_date) as post_month_,YEAR(post_date) as post_year");
		$this->db->select('MONTHNAME(post_date) as post_month');
		$this->db->join($this->users.' u','u.EmployeeID=p.EmployeeID','INNER');
		$this->db->where('post_headline',0);
		if($week=='now')
			$this->db->where("WEEK(post_date)= WEEK(curdate())");
		else
			$this->db->where("WEEK(post_date)<> WEEK(curdate())");
			//$this->db->where("WEEK(post_date)< WEEK(curdate())");
			
		if($category)
			$this->db->where('post_category',$category);
		$this->db->order_by('post_date','DESC');
		if($limit)
			$this->db->limit($limit);
		$Q=$this->db->get($this->posts.' p');
		return $Q->result_array();
	}
	
	function getImageByPost($id){
		$this->db->select("i.image_name");
		$this->db->where('post_id',$id);
		$Q=$this->db->get($this->images.' i');
		return $Q->result_array();
	}
	
	function getPostID(){
		$this->db->select("MAX(p.post_id) as ID");	
		$Q=$this->db->get($this->posts.' p');
		$data = $Q->row_array();
		if($Q->num_rows())
			$ID = $data['ID'] + 1;
		else
			$ID = 0;
		return $ID;	
	}
	
	function getDataSingleRecord($id='',$main='',$year='',$month='',$day=''){
		$this->db->select("*,u.EmployeeNickName as user_name,DAY(post_date) as post_day");
		$this->db->select("MONTH(post_date) as post_month_,YEAR(post_date) as post_year");
		$this->db->select('MONTHNAME(post_date) as post_month');
		$this->db->join($this->users.' u','u.EmployeeID=p.EmployeeID','INNER');
		if($id)
			$this->db->where('post_id',$id);
		if($main)
			$this->db->where('post_headline',$main);
		if($year):
			$this->db->where('YEAR(post_date)',$year);
			$this->db->where('MONTH(post_date)',$month);
			$this->db->where('DAY(post_date)',$day);
		endif;
		$this->db->order_by('post_date','DESC');	
		$Q=$this->db->get($this->posts.' p',1);
		return $Q->row_array();
	}
	
	function savePost($image=''){
		$value =  array( 'post_id'		=>	$this->getPostID(),
						 'post_title'  	=>  $this->input->post('title'),
						 'post_image'	=>	$image,
						 'post_date'  	=>  date('Y-m-d H:i:s'),
						 'post_category'=>	$this->input->post('category'),
						 'post_content' =>	$this->input->post('content'),
						 'EmployeeID'	=>	$this->session->userdata('user_login'));
		$this->db->insert($this->posts,$value);				 
	}
	
	function saveImage($image){
		$value =  array('post_id' => $this->getPostID(),'image_name'=>$image);
		$this->db->insert($this->images,$value);	
	}
	
	function getCommentByPost($post){
		$this->db->select("*,u.EmployeeNickName as user_name,DAY(comment_date) as day");
		$this->db->select("MONTH(comment_date) as month_,YEAR(comment_date) as year");
		$this->db->select('MONTHNAME(comment_date) as month');
		$this->db->join($this->users.' u','u.EmployeeID=p.EmployeeID','INNER');
		$this->db->where('comment_post',$post);
		$this->db->order_by('comment_date','ASC');
		$Q=$this->db->get($this->comment.' p');
		return $Q->result_array();
	}
	
	function saveComment(){
		$value =  array( 'comment_post' =>  $this->input->post('id'),
						 'EmployeeID'	=>	$this->session->userdata('user_login'),
						 'comment_date' =>  date('Y-m-d H:i:s'),
						 'comment_content'=>$this->input->post('comment'));
		$this->db->insert($this->comment,$value);				 
	}
	
	function updatePost($image=''){
		$value =  array( 'post_title'  	=>  $this->input->post('title'),
						 'post_category'=>	$this->input->post('category'),
						 'post_content' =>	$this->input->post('content'),
						 'EmployeeID'	=>	$this->session->userdata('user_login'));
		if(!empty($image))
			$value['post_image'] =$image;				 
		$this->db->where('post_id',$this->input->post('id'));				 
		$this->db->update($this->posts,$value);				 
	}
	
	function removePost($id){
		$this->db->where('post_id',$id);				 
		$this->db->delete($this->posts);				 
	}
	
	function removeComment($id){
		$this->db->where('comment_id',$id);				 
		$this->db->delete($this->comment);				 
	}
	
	function removeCommentByPost($id){
		$this->db->where('comment_post',$id);				 
		$this->db->delete($this->comment);				 
	}
	
	public function inserttodb()
    {
        if(!empty($_POST))
        {
            $username = $this->session->userdata('user_login');
			$name     =  $this->session->userdata('user_name');
            $date     = date('Y-m-d H:i:s');
            $message  = $this->input->post('message');
                
            $commentArray = array(
                'EmployeeID'    =>   $username,
				'post_category' =>   3,
                'post_date'     =>   $date,
                'post_content'  =>   $message
            );
            $this->db->insert('posts',$commentArray);
			
			$content = array('EmployeeLastStatus'=>$message);
			$this->db->where('EmployeeID',$username);
			$this->db->update('employee',$content);
			
            return $this->returnMarkup($name,$date,$message);
        }
           
            
    }
    private function returnMarkup($username,$date,$message)
         {
            $html = '<div class="author-box">
                    <div class="author-description">
                		<div class="author-image"><img src="'.base_url('assets/images/temp/avatar.jpg').'" alt=""><span class="author">'.$username.'</span></div>
                      	<div class="author-text">
                            <p><span style="font-size:14px;font-weight:bold">'.$date.'</span> <br/>'.word_limiter($message,50).'</p>
                        </div>
                        <div class="clear"></div>
                	</div>        
                    </div>';
			$html .= '<div class="add-comment contact-form">
                            
                            <div class="comment-form2">
                            <form action="#" method="post" class="ajax_form" id="contactForm">
                                  
                                <div class="row">
                                    <label class="label_title"><strong>Your Comment</strong></label>
                                    <textarea cols="30" rows="5" style="height:50px" name="message" id="message" class="textarea textarea_middle required"></textarea>
                                </div>
                                
                                <div class="row rowSubmit" style="text-align:right;width:580px">
	                                <input type="submit" name="btnsubmit" id="send" value="Your Comment" class="btn-submit">
                             	</div>
                            </form>
                            </div>
                        </div>';			
            return $html;
    }
		 
	public function inserttoComment()
    {
        if(!empty($_POST))
        {
            $username = $this->session->userdata('user_login');
			$name     =  $this->session->userdata('user_name');
            $date     = date('Y-m-d H:i:s');
			$post     = $this->input->post('post');
            $message  = $this->input->post('comment');
                
            $commentArray = array(
                'EmployeeID'     =>   $username,
				'comment_post'   =>   $post,
                'comment_date'   =>   $date,
                'comment_content'=>   $message
            );
            $this->db->insert('post_comment',$commentArray);
            return $this->returnMarkup2($name,$date,$message);
        }
           
            
    }
         private function returnMarkup2($username,$date,$message)
         {
            $html = '<div class="author-box" style="width:600px;margin-left:20px">
                    <div class="author-description" >
                		<div class="author-image"><img src="'.base_url('assets/images/temp/avatar.jpg').'" alt=""><span class="author">'.$username.'</span></div>
                      	<div class="author-text">
                            <p><span style="font-size:14px;font-weight:bold">'.$date.'</span> <br/>'.word_limiter($message,50).'</p>
                        </div>
                        <div class="clear"></div>
                	</div>        
                    </div>';		
            return $html;
         }
}